package com.viethp.mt.activities;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.GlobalVariables;
import com.viethp.mt.objects.ForgotPWDResponse;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AForgotPWD extends Activity implements OnClickListener{
	
	private Button btn_back, btn_done;
	private ProgressDialog progressDialog;
	private EditText edt_forgot_pwd;
	private String email;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_forgot_pwd);
		
		initComponents();
		setOnClickListener();
	}
	
	public void initComponents(){
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_done = (Button) findViewById(R.id.btn_done);
		edt_forgot_pwd = (EditText) findViewById(R.id.edt_forgot_pwd);
		
		progressDialog = new ProgressDialog(AForgotPWD.this);
		progressDialog.setCancelable(false);
	}
	public void setOnClickListener(){
		btn_back.setOnClickListener(this);
		btn_done.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			this.finish();
			break;
		case R.id.btn_done:
			if(checkValid()){
				email = String.valueOf(edt_forgot_pwd.getText()).trim();
				doForgot();
			}
			break;
		default:
			break;
		}
		
	}
	
	public boolean checkValid(){
		if(String.valueOf(edt_forgot_pwd.getText()).trim().equals("")) {
			CommonHelper.showWarning(this, "Please input your email!");
			edt_forgot_pwd.requestFocus();
			return false;
		}
		else if(!CommonHelper.eMailValidation(String.valueOf(edt_forgot_pwd.getText()).trim())) {
			CommonHelper.showWarning(this, "Invailid email address");
			edt_forgot_pwd.requestFocus();
			return false;
		}
		return true;
	}
	
	private void doForgot(){
		if(CommonHelper.isNetworkAvailable(this)){
			startCallService();
		} else CommonHelper.showWarning(AForgotPWD.this,
				getString(R.string.netstate));
	}
	
	public void startCallService(){
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.obj = callService();
				handler.sendMessage(message);
				
			}
		}).start();
	}
	
	private ForgotPWDResponse callService() {

		SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_FORGOT_PWD);
		userRequest.addProperty("email", email);

		SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_FORGOT_PWD);
		request.addProperty(GlobalVariables.WS_METHOD_FORGOT_PWD, userRequest);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				GlobalVariables.WS_ENDPOINT,
				GlobalVariables.WS_CONNECTTION_TIME_OUT);
		SoapObject results = null;
		try {
			androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
					+ GlobalVariables.WS_METHOD_FORGOT_PWD, envelope);
		} catch (Exception e) {
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_FORGOT_PWD, envelope);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;

			}
		}
		try {
			results = (SoapObject) envelope.getResponse();
		} catch (SoapFault ex3) {
			ex3.printStackTrace();
			return null;
		}
		
		return new ForgotPWDResponse(results);

	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			if (msg.obj != null) {
				ForgotPWDResponse response = (ForgotPWDResponse) msg.obj;
				if (response.getErrorCode() != null) {
					CommonHelper.showWarning(AForgotPWD.this, response
							.getErrorCode().getErrMsg());
				} else {
					CommonHelper.showToast(AForgotPWD.this, getString(R.string.forgot_password_email_sent));
				}

			} else {
				CommonHelper.showWarning(AForgotPWD.this,
						getString(R.string.disconnect));
			}

		};
	};
}
